%Script
%Thomas Stevens - Lim Lab
%opened 1/9/12
%Quantify average CFP inside GFP-containing pixels (voxels)

basepath = '../All_Lim_Microscopy/Yeast/010413_tethers_cluster_microscopy/';
n=3;

IS = struct('strains',{ ...
        'yTS30-Hy1', ...%'yTS30-Hy1DB', ...
        'yTS32-Hy1', ...
        'yTS32-Hy1DB' ...
    }, ...
    'raf_path',{ ...
            [basepath '010413_tethers_yTS30_nogal.nd2'], ...%[basepath '010413_tethers_yTS30db_nogal-1gal.nd2'] ...
            [basepath '010413_tethers_yTS32_nogal.nd2'], ...
            [basepath '010413_tethers_yTS32db_nogal.nd2'] ...
            }, ...
    'raf_I',cell(1,n), ...
    'raf_BW',cell(1,n), ...
    'raf_cInt',cell(1,n), ...
    'raf_cArea',cell(1,n), ...
    'raf_cPx',cell(1,n), ...
    'gal_path',{ ...
            [basepath '010413_tethers_yTS30_1gal.nd2'], ...%[], ...
            [basepath '010413_tethers_yTS32_1gal.nd2'], ...
            [basepath '010413_tethers_yTS32db_1gal.nd2'] ...
            }, ...
    'gal_I',cell(1,n), ...
    'gal_BW',cell(1,n), ...
    'gal_cInt',cell(1,n), ...
    'gal_cArea',cell(1,n), ...
    'gal_cPx',cell(1,n) ...
        );
           
    
for i=1:length(IS)
    [IS(i).raf_I IS(i).raf_BW] = read_nd2(IS(i).raf_path);
    [IS(i).raf_cInt,IS(i).raf_cArea,IS(i).raf_cPx] = extractMetrics(IS(i).raf_I,IS(i).raf_BW,1:4,[],[],100);
    [IS(i).gal_I IS(i).gal_BW] = read_nd2(IS(i).gal_path);
    [IS(i).gal_cInt,IS(i).gal_cArea,IS(i).gal_cPx] = extractMetrics(IS(i).gal_I,IS(i).gal_BW,1:4,[],[],100);
end

%Example view
%playStack(IS(1).raf_BW,1,1,2:4);

%Consider largest cluster in field, compare CFP per GFP, gal/nogal
%c_where_g is [raf/gal,index,512,512]
for i=1:length(IS)
    %raf
    [max_area,max_i] = max(IS(i).raf_cArea(1,:,:,:));
    c_where_g_px(1,i,:,:) = uint16(IS(i).raf_BW{2,1}) .* IS(i).raf_cPx{4,1,max_i};
    c_where_g(1,i) = sum(sum(c_where_g_px(1,i,:,:)))/sum(IS(i).raf_BW{2,1}(:));
    %gal
    [max_area,max_i] = max(IS(i).gal_cArea(1,:,:,:));
    c_where_g_px(2,i,:,:) = uint16(IS(i).gal_BW{2,1}) .* IS(i).gal_cPx{4,1,max_i};
    c_where_g(2,i) = sum(sum(c_where_g_px(2,i,:,:)))/sum(IS(i).gal_BW{2,1}(:));
end